<template>
  <div>
    <Page ref="page" :search-config="searchConfig" :table-config="tableConfig" :toolbar-config="toolbarConfig" />
    <region-add ref="regionAdd" @save-success="addSuccess" />
    <region-update ref="regionUpdate" @save-success="updateSuccess" />
  </div>
</template>
<script setup lang="ts">
import { getRegionPageList, delRegion } from "@/api/warehouse/regionApi";
import { MPage as Page } from "page-com";
import { PageSearchConfig, PageTableConfig, ToolbarConfig, type PageInstance } from "page-com";
import { reactive, shallowReactive, useTemplateRef } from "vue";
import RegionAdd from "./RegionAdd.vue";
import { msgConfirm, successMsg } from "@/utils/message";
import RegionUpdate from "./RegionUpdate.vue";
import { hasPerms } from "@/utils/auth";
import Enums from "@/enums/regionEnum";
import { useOffineIcon } from "@/components/ReIcon/src/hooks";
const searchConfig = shallowReactive<PageSearchConfig>({
  pageSearchFrame: [
    {
      frameType: "input",
      key: "localName",
      label: "名称"
    },
    {
      frameType: "datetimerange",
      key: "createTime",
      searchKey: ["createTimeStart", "createTimeEnd"],
      label: "创建时间"
    }
  ],
  searchFun: getRegionPageList
});
const tableConfig = reactive<PageTableConfig>({
  pageTableFrame: [
    {
      key: "localName",
      label: "名称"
    },
    {
      key: "regionPathName",
      label: "路径"
    },
    {
      key: "regionGrand",
      label: "级别",
      render(data: number) {
        return Enums.RegionGrandEnums.find(v => v.id == data)?.text;
      }
    },
    {
      key: "createTime",
      label: "创建时间"
    },
    {
      key: "updateTime",
      label: "修改时间"
    },
    {
      frameType: "multi",
      key: "multi",
      label: "操作",
      separate: "",
      multi: [
        {
          frameType: "link",
          value: "删除",
          key: "deleteBtn",
          icon: useOffineIcon("ep:delete"),
          click: showDel,
          visible: hasPerms("/warehouse/region/del")
        },
        {
          frameType: "link",
          value: "修改",
          key: "updateBtn",
          icon: useOffineIcon("ep:edit"),
          click: showUpdate,
          visible: hasPerms("/warehouse/region/update")
        }
      ]
    }
  ]
});
const toolbarConfig = shallowReactive<ToolbarConfig>({
  buttons: [
    {
      value: "添加",
      click: showAdd,
      visible: hasPerms("/warehouse/region/add")
    }
  ]
});
const regionAdd = useTemplateRef("regionAdd");
const page = useTemplateRef<PageInstance>("page");
function showAdd() {
  regionAdd.value.show();
}
const regionUpdate = useTemplateRef("regionUpdate");
function showUpdate(vo: ShopRegionVo) {
  regionUpdate.value.show(vo);
}

function addSuccess() {
  page.value.onSearch();
}
function updateSuccess() {
  page.value.onSearch();
}

function showDel(vo: ShopRegionVo) {
  msgConfirm("确认删除吗？", async () => {
    let res = await delRegion({ id: vo.id });
    if (res.code == RESPONSE_CODE.SUCCESS) {
      successMsg("删除成功");
      page.value.onSearch();
    }
    return res;
  });
}
</script>
